<?php
namespace app\admin\controller;

use app\admin\model\ClassroomModel;
use app\admin\model\School as SchoolModel;
use app\admin\Model\MajorModel;
use app\admin\Model\ScoreModel;
use app\admin\Model\StudentModel;
use app\admin\Model\TermModel;
use app\admin\Model\CourseModel;
use cmf\controller\AdminBaseController;
use think\db\Where;
use think\Validate;

class ScoreController extends AdminBaseController{
    public function index(){
        extract(input());
        $where = [];
        $hasWhere = [];
        if (isset($name) && !empty($name)){
            $hasWhere[] = ['name','like','%'.$name.'%'];
        }
        if(isset($term_id) && !empty($term_id)){
            $where[] = ['term_id','=',$term_id];
        }

        if(isset($subject_id) && !empty($subject_id)){
            $where[] = ['subject_id','=',$subject_id];
        }
        $data = ScoreModel::hasWhere('student',$hasWhere)
        ->with(['student','term','course'])->where($where)->order('id Desc')->paginate(3);
       //halt($data);
        $page = $data->render();
        $term = TermModel::all();
        $subject = CourseModel::all();
        return $this->fetch('',[
            'page' => $page,
            'data' => $data,
            'term' => $term,
            'subject' => $subject
        ]);

    }
    public function Get_major()
    {
        $schoolId = input('school_id');
        $major = MajorModel::where('school_id', $schoolId)
            ->select();
        $options = ["<option value=''>请选择专业</option>"];
        foreach ($major as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['major_name'] . "</option>";
        }
        return $options;
    }

    public function Get_class()
    {
        $majorID = input('major_id');
        $major = ClassroomModel::where('major_id', $majorID)
            ->select();
        $options = ["<option value=''>请选择班级</option>"];
        foreach ($major as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['class_name'] . "</option>";
        }
        return $options;
    }
    public function Get_student()
    {
        $studentID = input('class_id');
        $student = StudentModel::where('class_id', $studentID)
            ->select();
        $options = ["<option value=''>请选择学生</option>"];
        foreach ($student as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['name'] . "</option>";
        }
        return $options;
    }
    public function add() {
        $school = SchoolModel::all();
        $data = StudentModel::all();
        $Term = TermModel::all();
        $date = CourseModel::all();
        $this->assign('date',$date);
        $this->assign('school',$school);
        $this->assign('data',$data);
        $this->assign('term',$Term);
        return $this->fetch();
    }
    public function addPost() {
        $data = input();
        $rule = [
            'sid' => 'require',
            'term_id' => 'require',
            'subject_id' => 'require',
            'score' => 'require|float'
        ];

        $msg = [
            'sid.require' => '请选择学生',
            'term_id.require' => '请选择学期',
            'subject_id.require' => '请选择科目',
            'score.require' => '请输入分数',
            'score.float' => '分数必须是浮点型'
        ];

        $validate = Validate::make($rule, $msg);
        $result = $validate->check($data);

        if (true !== $result) {
            $this->error($validate->getError());
        }

        $has = ScoreModel::where([
            'sid' => $data['sid'],
            'term_id' => $data['term_id'],
            'subject_id' => $data['subject_id']
        ])->find();

        if ($has) {
            $this->error('所选学生、学期和科目已存在成绩记录！');
        }
        $add = ScoreModel::create($data);
        if ($add){
            return json(['code' => 1 ,'msg' => '添加成功']);
        }else{
            return json (['code' => 2 ,'msg' => '添加失败']);
        }
    }
    public function edit()
    {
        $id = input('id');

        // 查询要修改的数据
        $score =ScoreModel::where('id', $id)
            ->find();
        $this->assign('score', $score);

        $course=CourseModel::all();
        $this->assign('course',$course);

        $term=TermModel::all();
        $this->assign('term',$term);

        $student=StudentModel::all();
        $this->assign('student',$student);

        $student =StudentModel::find($id);
        $school =SchoolModel::all();

//        $classId =StudentModel::where('id', $score['student_id'])->value('class_id');
        // 找到该学生所在专业 ID
        $majorId =ClassroomModel::where('id', $student['class_id'])->value('major_id');
        // 找到该学生所在学校的 ID
        $schoolId =MajorModel::where('id', $majorId)->value('school_id');
        // 查询该学生所在学校下的专业
        $major =MajorModel::where('school_id', $schoolId)->select();
        // 查询该学生所在专业下的班级
        $class =ClassroomModel::where('major_id', $majorId)->select();
        // 查询该班级下的学生
//        $students =StudentModel::where('class_id', $classId)->select();
        $this->assign('id', $id);
        return $this->fetch('', [
            'data' => $student,
//            'student' => $students,
            'school' => $school,
            'majorId' => $majorId,
            'schoolId' => $schoolId,
            'major' =>$major,
            'class' => $class
        ]);
    }
    public function index_student(){
        $sid = input('sid');

        extract(input());

        $where = new Where();
        $hasWhere = [];
        $where['sid'] = $sid;

        if (isset($term_id) && !empty($term_id)) {
            $where['term_id'] = $term_id;
        }
        if (isset($name) && !empty($name)){
            $hasWhere[] = ['name','like','%'.$name.'%'];
        }
        if (isset($subject_id) && !empty($subject_id)) {
            $where['subject_id'] = $subject_id;
        }

        $data = ScoreModel::hasWhere('student',$hasWhere)
            ->where($where)
            ->with('student,term,course')
            ->order('id DESC')
            ->paginate(10, false, ['query' => request()->param()]);

        $page = $data->render();

        $term = TermModel::all();
        $subject = CourseModel::all();

        return $this->fetch('', [
            'data' => $data,
            'page' => $page,
            'term' => $term,
            'course' => $subject,
            'sid' => $sid
        ]);
    }
    public function add_student(){
        $rule = [
            'sid' => 'require',
            'term_id' => 'require',
            'subject_id' => 'require',
            'score' => 'require|float'
        ];

        $msg = [
            'sid.require' => '请选择学生',
            'term_id.require' => '请选择学期',
            'subject_id.require' => '请选择科目',
            'score.require' => '请输入分数',
            'score.float' => '分数必须是浮点型'
        ];
        $sid = input('sid');
        $student = StudentModel::where('id',$sid)->find();
        $Term = TermModel::all();
        $date = CourseModel::all();
        $this->assign('date',$date);
        $this->assign('student',$student);
        $this->assign('term',$Term);
        $this->assign('sid',$sid);
        return $this->fetch();
    }
    public function editPost(){
        $data = input();
        $rule = [
            'sid'  => 'require',
        ];
        $msg = [
            'sid.require' => '姓名不能为空',
        ];
        $validate = new Validate($rule, $msg);
        $result   = $validate->check($data);
        if (true!==$result){
            $this->error($validate->getError());
        }
        $edit = ScoreModel::where('id',$data['id'])->update($data);
        if ($edit) {
            return json(['code' => 1 , 'msg' => '修改成功']);
        }else{
            return json(['code' => 2 , 'msg' => '修改失败']);
        }
    }
    public function delete () {
        $id = input('id');
        $del = ScoreModel::destroy($id);
        if($del){
            $this->success('删除成功','score/index');
        }else{
            $this->error('删除失败','score/index');
        }
    }
    public function delAll() {
        $ids = input('ids/a');
        $del = ScoreModel::destroy($ids);
        if($del){
            $this->success('删除成功','score/index');
        }else{
            $this->error('删除失败','score/index');
        }
    }
}